package sendmail;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

public class ViewContent extends HttpServlet {
	private static final Logger log = Logger.getLogger(ViewContent.class
			.getName());

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String lang = req.getParameter(Constants.LANG);
		String subject = "";
		String body = "";
		try {
			DatastoreService dataService = DatastoreServiceFactory
					.getDatastoreService();

			Key key = KeyFactory.createKey(Constants.KIND_MAIL_CONTENT, lang);
			Entity entity = dataService.get(key);
			subject = (String) entity.getProperty(Constants.SUBJECT);
			Object bodyProp = entity.getProperty(Constants.BODY);
			if (bodyProp instanceof Text) {
				body = ((Text) bodyProp).getValue();
			} else {
				body = (String) bodyProp;
			}

		} catch (EntityNotFoundException e) {
			log.log(Level.SEVERE, e.getLocalizedMessage());
		}
		req.setAttribute(Constants.SUBJECT, subject);
		req.setAttribute(Constants.BODY, body);
		req.setAttribute(Constants.LANG, lang);
		req.getRequestDispatcher("/admin/mail_contents.jsp").forward(req, resp);
	}
}
